﻿using NewfarTormentTool.core;
using NewfarTormentTool.data;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace NewfarTormentTool.control
{
    /// <summary>
    /// 角色卡片控件
    /// </summary>
    public partial class RoleItemControl : UserControl
    {
        public RoleItemControl()
        {
            InitializeComponent();
            CB_RoleName.BindingContextChanged += new EventHandler(CB_RoleName_TextChanged);
            Label_RoleName.BindingContextChanged += new EventHandler(Label_RoleName_TextChanged);
        }

        /// <summary>
        /// 角色配置
        /// </summary>
        private RoleConfig Config;

        /// <summary>
        /// 设置角色
        /// </summary>
        /// <param name="config">配置</param>
        /// <param name="active">是否激活</param>
        /// <param name="show">是否纯显示</param>
        public void SetUpRole(RoleConfig config, bool active, bool show = false)
        {
            Config = config;
            Image_Head.Image = Image.FromFile(Path.Combine(Directory.GetCurrentDirectory(), "img", "r" + config.id + ".jpg"));
            if (show)
            {
                CB_RoleName.Visible = false;
                Label_RoleName.Visible = true;
                Label_RoleName.Text = config.ShowName("zh-cn");
            }
            else
            {
                CB_RoleName.Visible = true;
                CB_RoleName.Text = config.ShowName("zh-cn");
                CB_RoleName.Checked = active;
                Label_RoleName.Visible = false;
            }
        }

        /// <summary>
        /// 勾选框文本变更事件
        /// </summary>
        private void CB_RoleName_TextChanged(object sender, EventArgs e)
        {
            int x = (Width - CB_RoleName.Width) / 2;
            CB_RoleName.Location = new Point(x, CB_RoleName.Location.Y);
        }
        
        /// <summary>
        /// 纯文本变更事件
        /// </summary>
        private void Label_RoleName_TextChanged(object sender, EventArgs e)
        {
            int x = (Width - Label_RoleName.Width) / 2;
            Label_RoleName.Location = new Point(x, Label_RoleName.Location.Y);
        }

        /// <summary>
        /// 选项变更
        /// </summary>
        private void CB_RoleName_CheckedChanged(object sender, EventArgs e)
        {
            bool active = CB_RoleName.Checked;
            RoleDataStorage.Instance.ChangeUserRoleActive(Config.id, active);
        }
    }
}
